package xcu.lxj.nettychat.chat.consumer;

import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import xcu.lxj.nettychat.chat.constant.MQConstant;
import xcu.lxj.nettychat.chat.domain.dto.PushMessageDTO;
import xcu.lxj.nettychat.chat.domain.enums.WSPushTypeEnum;
import xcu.lxj.nettychat.user.service.inter.WebSocketService;


/**
 * 完成消息推送给 房间成员
 * @Author XinJian Lei
 * @Date 2024/9/26 16:32
 * @Version 1.0
 */
@RocketMQMessageListener(consumerGroup = MQConstant.PUSH_GROUP, topic = MQConstant.PUSH_TOPIC)
@Component
public class PushConsumer implements RocketMQListener<PushMessageDTO> {


    @Autowired
    WebSocketService webSocketService;

    @Override
    public void onMessage(PushMessageDTO message) {

        WSPushTypeEnum wsPushTypeEnum = WSPushTypeEnum.of(message.getPushType());

        switch (wsPushTypeEnum) {
            case USER:
                message.getUidList().forEach(uid -> {
//              发送消息
                    webSocketService.sendToUid(message.getWsBaseMsg(), uid);
                });
                break;
            case ALL:
//              发送给全部用户
                webSocketService.sendToAllOnline(message.getWsBaseMsg(), null);
                break;
        }


    }
}
